
****************************************************************************
/**** This do-file runs regressions on the monetary policy episodes *****/
****************************************************************************

/* The purpose of this do-file is building Tables 9, 10, 11 and upper panels
 of Tables 12 and 13. In all these tables, regressions run on multiple bank relationship firms  */
 
*********************************
* Loading ADOs and Pseudo DTA
********************************
clear all
clear matrix
clear mata
clear results
set more off, permanently

* There is nothing special in my ado folder. I consider reghdfe, outreg2, winsor2, center and unique (as well as its dependencies) enough to run this code.

adopath + "//sbcdf060/Depep01$/ado"
adopath ++ "//sbcdf060/Depep01$/ado"
global dta_file = "D:\Countercyclical\Dta_files"
global output_file = "D:\Countercyclical\Output_files"


* This file contains Pseudo Data for the two monetary policy episodes (easing and tightening)
use "$dta_file\PseudoData_MPs.dta", clear



/***********************************
**** Prepare the working sample ****
***********************************/

*** Eliminate defaulted loans
drop if L3_firm_default==1

*** Eliminate new banks, so that all policy interventions have the same 91 banks 
keep if balance==0

*** Creating firms above and below the median of risk and size to split samples
bysort firm_id time: egen L3_firm_interest = pctile(L3_total_interest), p(50)
	
preserve 	

	tempfile z
	
	bysort firm_id time: gen first=_n==1
	keep if first==1
		
	
	bysort time : egen med_interest = pctile(L3_firm_interest), p(50)
	
	* Defining risky firms as above the median of paid interest rates before the policy
	gen  riskP50= ((L3_firm_interest - med_interest)>0)

	* Defining small firms as below the median of number of employees before the policy
	bysort time : egen med_emp = pctile(n_employees), p(50)

	gen  empP50= ((n_employees - med_emp)<0)
    drop first
	save `z', replace

restore 

merge m:1 firm_id time using `z', nogen keep(match)


*** Adjusting the sample for fixed effects. Only multiple bank relationship firms are used in the monetary policy exercises. 

*Restricting the sample at the loan-level to keep the number of observations constant across regressions while the number of controls increase 
local dep_var wd3ln_amount_bank
foreach d of local dep_var {

reghdfe `d'   $risk_controls    if time=="easing", absorb(firm_id bank_id  firm_industry2#municipality) res(easing)
reghdfe `d'   $risk_controls   if time=="tightening", absorb(firm_id bank_id firm_industry2#municipality) res(tightening)



keep if !mi(easing)|!mi(tightening)
drop easing tightening

}

*Restricting the sample at the firm-level to keep the same firms at the loan and firm-level regressions
preserve

      tempfile g
	  gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare2= abank/afirm
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare2
		bysort firm_id time: gen first = _n==1
		keep if first==1
		drop first
		
local dep_var wd3ln_amount_bank

foreach d of local dep_var {

	reghdfe `d'   $risk_controls   if time=="easing", absorb(firm_industry2#municipality max_bank ) res(easing)
	reghdfe `d'   $risk_controls    if time=="tightening", absorb(firm_industry2#municipality max_bank ) res(tightening)



	keep if !mi(easing)|!mi(tightening)
	
	}
     save `g', replace


restore 
merge m:1 firm_id time using `g', keepusing(easing tightening) keep(match) nogen
* The Sample is now ready



                         /***********************************
                          * Creating the Summary in Table 9 *
					     ************************************/ 	
							
* The main dependent variable, changes in credit exposure. This is simply the quarterly change of total credit for each firm-bank pair. Policy interventions are within the window. So, this reflects the change in "after" minus "before" the monetary policy shocks
label var dln3_amount_bank "Dln_loan"

* The Treatment variable, share on Noncore liabilities measured for each bank before the policy
label var nocore "Noncore"

* The additional dependent variable at the firm-level. Firm-level credit contains all credit provided by all financial institutions to firm "f". So, this reflects the change in "after" minus "before" the policy for each firm (and not firm-bank pair)
label var dln3_amount_firm "Dln_firm"

	
	** Panel A: Easing of Interest Rates
cd $output_file
	
	 estpost tabstat dln3_amount_bank nocore if time=="easing", ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table9A_LoanControls_MP_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Loan Controls_MP_easing") nonumber csv replace label
		
		
	preserve
		       *Lagged (ln) of loan-level credit exposure 	
    	        gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen mshare2= abank/afirm2
				* Treatment variable (which is bank-level) weighted at firm-level 
				gen nocore_firm = nocore*mshare2
				
				label var nocore_firm "Noncore"
				
		     keep if time=="easing"
			 
			 collapse (sum)  nocore_firm (mean) dln3_amount_firm, by(firm_id time )  
		
		estpost tabstat dln3_amount_firm nocore_firm, ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table9A_FirmControls_MP_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Firm Controls_MP_easing") nonumber csv replace label


restore 

preserve
    keep if time=="easing"
	bysort bank_id time: gen first=_n==1
	keep if first==1
			 
		estpost tabstat nocore, ///
		listwise statistics (min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table9A_BankControls_MP_easing", cells("min p10 p50 mean p90 max sd count") mtitles("Bank Controls_MP_easing") nonumber csv replace label


restore 		
** The estimates of these three levels are appended and edited manually from the three csv files just generated by this piece of code, in excel. 


	** Panel B: Tightening of Interest Rates
	
	 estpost tabstat dln3_amount_bank nocore if time=="tightening", ///
	listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample 
	esttab using "Table9B_LoanControls_MP_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Loan Controls_MP_tightening") nonumber csv  replace label
		
		
	preserve
		       *Lagged (ln) of loan-level, credit exposure 	
    	        gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen mshare2= abank/afirm2
				*Weighting bank-level var into firm-level
				gen nocore_firm = nocore*mshare2
				* Firm-level treatment variable
				label var nocore_firm "Noncore"
							
		         keep if time=="tightening"
			 
			 collapse (sum)  nocore_firm (mean) dln3_amount_firm, by(firm_id time )  
			
		
			estpost tabstat dln3_amount_firm nocore_firm, ///
			listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample
			esttab using "Table9B_FirmControls_MP_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Firm Controls_MP_tightening") nonumber csv replace label


restore 

preserve
    keep if time=="tightening"
	bysort bank_id time: gen first=_n==1
	keep if first==1
			 
		sum nocore
		estpost tabstat  nocore, ///
		listwise statistics(min p10 p50 mean p90 max sd count) columns(statistics) esample
		esttab using "Table9B_BankControls_MP_tightening", cells("min p10 p50 mean p90 max sd count") mtitles("Bank Controls_MP_tightening") nonumber csv replace label


restore 		
*** The estimates of these three levels are manually appended and edited in excel from the three csv files just generated by this piece of code . 

*End of Table 9*

	
                            ***********************
                            ***** Regressions *****
						    ***********************
							
** Set-up

* The dependent variable in Tables 9 and 10
global dep_var		    wd3ln_amount_bank 

* The Treatment variable for the monetary policy intervention 
global treat_var        wnocore

* The controls are built in the same way and take the same names as in the remaining files and codes
global bank_controls   wL3_capital wL3_liquidity wL3_ln_size  gov foreign small commercial wL3_npl_rodrigo  

global risk_controls         L3_collateral wL3_scline L3_foreign_currency wL3_ln_r_maturity wL3_total_interest

global firm_controls    wln_L3_amount_firm wL3_ln_employees   

* Two-way clustering
global clusters         bank firm_industry2
* Continous variables are winsorized before been loaded in the regressions. 



                            /**********************
                             * Creating Table 10 *
					        **********************/ 						 

gen n_firms =0 
label var wnocore "Noncore"
	
local dep_var $dep_var 
local time_var  easing tightening 
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
foreach v of local treat_var {
		
* Counting the number of firms in each regression
unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 1 and 5
reghdfe `d' `v' $risk_controls  $bank_controls4  if !mi(`t'), absorb(firm_id) vce(cluster $clusters)
outreg2 using Table10, excel append label ctitle(`t'_fullsample)  dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, N firms, `n_firms') nocons keep(`v')
	
/* Split by Riskiness of Firms  */ 

* Low-risk firms
preserve 

	keep if riskP50==0	

	unique firm_id if !mi(`t')
	replace n_firms = r(sum) if !mi(`t')


	sum n_firms if !mi(`t')
	local n_firms = r(mean)

* Column  2 and 6
	reghdfe `d' `v' $risk_controls  $bank_controls4  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
	outreg2 using Table10, excel append label ctitle(`t'_lowrisk) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, N firms, `n_firms') nocons keep(`v')


restore 

* High-risk firms

preserve 

	keep if riskP50==1

	unique firm_id if !mi(`t')
	replace n_firms = r(sum) if !mi(`t')


	sum n_firms if !mi(`t')
	local n_firms = r(mean)

* Column  3 and 7
	reghdfe `d' `v' $risk_controls  $bank_controls4  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
	outreg2 using Table10, excel append label ctitle(`t'_highrisk)  dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, N firms, `n_firms') nocons keep(`v')
		

restore

/* Split by Employment  */ 

*Large firms
preserve 

keep if empP50==0


	unique firm_id if !mi(`t')
	replace n_firms = r(sum) if !mi(`t')


	sum n_firms if !mi(`t')
	local n_firms = r(mean)

* Column  4 and 9
	reghdfe `d' `v' $risk_controls  $bank_controls4  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table10, excel append label ctitle(`t'_large) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, N firms, `n_firms') nocons keep(`v')


restore 

*Small firms

preserve 

keep if empP50==1

	unique firm_id if !mi(`t')
	replace n_firms = r(sum) if !mi(`t')


	sum n_firms if !mi(`t')
	local n_firms = r(mean)

* Column  5 and 10
	reghdfe `d' `v' $risk_controls  $bank_controls4  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table10, excel append label ctitle(`t'_small) dec(3) addtext(Loan Controls, Yes, Bank Controls, Yes, Firm FE, Yes, N firms, `n_firms') nocons keep(`v')
 

restore 

}
}
}
* End of Table 10


		                     /**********************
                             * Creating Table 11 *
					        **********************/ 			 

*Prepare firm FEs to use on firm-level regressions, i.e. the "credit demand proxy" for easing and tightening 
local dep_var $dep_var  
local time_var  easing tightening

foreach d of local dep_var  {
foreach t of local time_var {

reghdfe wd3ln_amount_bank $treat_var $risk_controls $bank_controls if !mi(`t') , absorb(f`t'=firm_id )

}
} 


				/*************************************
				  *Firm Level Regressions - Table 11
			    **************************************/

				preserve 
			
				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
		

				local r $risk_controls
		
				local risk_controls ""

				foreach v of local r  {
					
                * Weighting loan-level vars into firm-level
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
		
				local bank_controls  ""

				foreach v of global bank_controls  {
				    
				* Weighting bank-level vars into firm-level
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}

				 *Weighting the treatment variable (as in the Summary) into firm-level
				foreach v of var wnocore  {
				gen e_`v' = mshare2*`v'
				}
	
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				
				* max_bank is the bank against which the firm has the largest exposure
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2

				
				collapse (sum) `risk_controls'  ///
								  e_wnocore  ///	
							   `bank_controls'  ///
						 (max)  easing tightening  firm_industry2 municipality riskP50 empP50 ///
						 $firm_controls wd3ln_amount_firm  max_bank* ///
						 						   ln_emp feasing ftightening, by (firm_id time )                        
			
				   
				 keep if !mi(easing)|!mi(tightening)
				 

				local clusters max_bank firm_industry2 
				
				label var e_wnocore "Noncore"
		         * The dependent variable of Table 11
				label var wd3ln_amount_firm "d_ln_firm_credit*(100)"
				local dep_var wd3ln_amount_firm

				local treat_var easing tightening 

								
				sum $firm_controls

				cd $output_file
					
				foreach d of local dep_var {
				foreach t of local treat_var {
				* set-up sample
				reghdfe `d' e_wnocore  $firm_controls `bank_controls' if !mi(`t') , absorb(firm_industry2#municipality) res(re_`t')

				unique firm_industry2 if !mi(re_`t')
				gen n_sectors = r(sum)

				unique municipality if !mi(re_`t')
				gen n_counties = r(sum)

				unique max_bank if !mi(re_`t')
				gen n_banks = r(sum)


				sum n_sectors
				local n_sectors = r(mean)

				sum n_counties
				local n_counties = r(mean)

				sum n_banks
				local n_banks = r(mean)


				gen f = f`t'
				label var f "credit demand proxy"
			    foreach dep of var  wd3ln_amount_firm {

		
		        * col 1 and 3
				reghdfe `dep' e_wnocore `risk_controls' $firm_controls  `bank_controls' if !mi(re_`t') , absorb(firm_industry2#municipality) vce(cluster `clusters')
				outreg2 using Table11, excel append label ctitle(`t' Dln_credit)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes, ///
				N sectors, `n_sectors', N counties, `n_counties')  nocons 
				* col 2 and 4
				reghdfe `dep' e_wnocore f `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(firm_industry2#municipality) vce(cluster `clusters')
					outreg2 using Table11, excel append label ctitle(`t' Dln_credit)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext(Loan Controls, Yes, Firm Controls, Yes, Bank Controls, Yes,  Sector*County FE, Yes, ///
				N sectors, `n_sectors', N counties, `n_counties')  nocons 
		
								
				}
	

				drop n_sectors n_banks n_counties
				drop f
				}
				}


	restore
				
* End of Table 11



         /*************************************************************
	     *Creating  Table 12A: Table for Policy Comparison on Easings *
	     *************************************************************/
		 
drop n_firms 
gen n_firms =0 

	
local dep_var $dep_var  
local time_var  easing
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
foreach v of local treat_var {
		


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')


gen c_`v'= `v' if !mi(`t')
sum c_`v' if !mi(`t'),d


* We adjust treatment variable c_`v' so that the estimated parameter reflects one standard deviation over 1 p.p. (and not 0.75 p.p.) monetary policy interventions. This exercise is for comparability with RR policy intervention in Panel B.

replace c_`v' = (`v'*0.75)/(r(sd)) if !mi(`t')
gen Noncore = c_`v' if !mi(`t')
label var Noncore "Noncore"

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 1
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12A, excel append label ctitle(Fullsample) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)
	
/* Split by Riskiness of Firms  */ 

* Low-risk firms
preserve 

keep if riskP50==0	


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 2
reghdfe `d' Noncore $risk_controls  $bank_controls   if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12A, excel append label ctitle(Low-Risk) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)



restore 

** High-risk firms
preserve 

keep if riskP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')



sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 3
reghdfe `d' Noncore $risk_controls  $bank_controls   if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12A, excel append label ctitle(High-risk) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)
	

restore

/* Split by Employment  */ 

* Large firms
preserve 

keep if empP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')



sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 4
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12A, excel append label ctitle(Large) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)


restore 

*Small firms
preserve 

keep if empP50==1


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')


sum n_firms if !mi(`t')
local n_firms = r(mean)

* Column 5
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table12A, excel append label ctitle(Small)  dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)



restore 
drop c_`v'
drop Noncore

	}
}
}


				/****************
				*Firm Level 
				**************
				***************/

				preserve 

				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
	

				local r $risk_controls
			
				local risk_controls ""

				foreach v of local r  {
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
				

				local bank_controls ""

				foreach v of global bank_controls  {
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}


				 
				foreach v of var wnocore  {
				gen e_`v' = mshare2*`v'
				}

			
		
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2

				collapse  (sum) `risk_controls'  ///
								  e_wnocore   ///	
							   `bank_controls'  ///
						 (max)  easing tightening  firm_industry2 municipality riskP50 empP50 ///
						 $firm_controls wd3ln_amount_firm  max_bank* ///
						 						 ln_emp feasing ftightening, by ( firm_id time )                        
			
				   
				 keep if !mi(easing)| !mi(tightening)
		
				local clusters max_bank firm_industry2 
				
				label var feasing "credit demand proxy"
				

				local dep_var wd3ln_amount_firm 
				local time_var easing			
				
				

				cd $output_file
					
				foreach d of local dep_var {
				foreach t of local time_var {

				reghdfe `d' e_wnocore  $firm_controls `bank_controls' if !mi(`t') , absorb(firm_industry2#municipality) res(re_`t')

				unique firm_id if !mi(re_`t')
				gen n_firms = r(sum)


				sum n_firms
				local n_firms = r(mean)

    	
			    foreach dep of var  wd3ln_amount_firm {

				

sum e_wnocore if !mi(re_`t') ,d
replace e_wnocore = e_wnocore*0.75/r(sd) if !mi(re_`t')
gen Noncore = e_wnocore if !mi(re_`t') 
label var Noncore "Noncore"


				* Column 6
				reghdfe `dep' Noncore f`t' `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(firm_industry2#municipality) vce(cluster `clusters')
				outreg2 using Table12A, excel append label ctitle(Firm-level)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext( N firms, `n_firms') nocons	
				
		
								
				}				 
				}
				}



				restore
			
*End of Table 12A



         /*************************************************************
	     *Creating  Table 13A: Table for Policy Comparison on Tightenings *
	     *************************************************************/
drop n_firms
gen n_firms =0 

	
local dep_var $dep_var  
local time_var  tightening
local treat_var $treat_var 


foreach d of local dep_var {
foreach t of local time_var {
foreach v of local treat_var {
		


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')


gen c_`v'= `v' if !mi(`t')
sum c_`v' if !mi(`t'),d


* We adjust treatment variable c_`v' so that the estimated parameter reflects one standard deviation over 1 p.p. (and not 0.75 p.p.) monetary policy intervention. This exercise is for comparability with RR policy intervention in Panel B.

replace c_`v' = (`v'*0.75)/(r(sd)) if !mi(`t')
gen Noncore = c_`v' if !mi(`t')
label var c_`v' "Noncore"	
label var Noncore "Noncore"

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Col 1
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13A, excel append label ctitle(Fullsample) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)

/* Split by Riskiness  */ 	

* Low-Risk
preserve 

keep if riskP50==0	


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')

sum n_firms if !mi(`t')
local n_firms = r(mean)

* Col 2
reghdfe `d' Noncore $risk_controls  $bank_controls   if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13A, excel append label ctitle(Low-Risk) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)



restore 

* High-Risk
preserve 

keep if riskP50==1

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')



sum n_firms if !mi(`t')
local n_firms = r(mean)

* Col 3
reghdfe `d' Noncore $risk_controls  $bank_controls   if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13A, excel append label ctitle(High-risk) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)
	

restore

/* Split by Employment  */ 

* Large
preserve 

keep if empP50==0

unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')



sum n_firms if !mi(`t')
local n_firms = r(mean)

* Col 4
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13A, excel append label ctitle(Large) dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)



restore 

*  Small
preserve 

keep if empP50==1


unique firm_id if !mi(`t')
replace n_firms = r(sum) if !mi(`t')


sum n_firms if !mi(`t')
local n_firms = r(mean)

* Col 5
reghdfe `d' Noncore $risk_controls  $bank_controls  if !mi(`t') , absorb(firm_id) vce(cluster $clusters)
outreg2 using Table13A, excel append label ctitle(Small)  dec(3) addtext(N firms, `n_firms') nocons keep(Noncore)



restore 
drop c_`v'
drop Noncore

	}
}
}


			
				/****************
				*Firm Level 
				**************
				***************/

				preserve 

				gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare= abank/afirm
				gen mshare2= abank/afirm2
				drop afirm 
	

				local r $risk_controls
			
				local risk_controls ""

				foreach v of local r  {
				gen e_`v' = mshare*`v'
				local risk_controls `risk_controls' e_`v'
				}

				local risk_controls `risk_controls'
				

				local bank_controls ""

				foreach v of global bank_controls  {
				gen e_`v' = mshare2*`v'
				local bank_controls `bank_controls' e_`v'
				}


				 
				foreach v of var wnocore  {
				gen e_`v' = mshare2*`v'
				}

			
		
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare mshare2

				collapse  (sum) `risk_controls'  ///
								  e_wnocore   ///	
							   `bank_controls'  ///
						 (max)  easing tightening  firm_industry2 municipality riskP50 empP50 ///
						 $firm_controls wd3ln_amount_firm  max_bank* ///
						 						 ln_emp feasing ftightening, by ( firm_id time )                        
			
				   
				 keep if !mi(easing)| !mi(tightening)
		
				local clusters max_bank firm_industry2 
				
				label var ftightening "credit demand proxy"
				
				local dep_var wd3ln_amount_firm
				local time_var tightening			
				
				
				sum $firm_controls

				cd $output_file
					
				foreach d of local dep_var {
				foreach t of local time_var {

				reghdfe `d' e_wnocore  $firm_controls `bank_controls' if !mi(`t') , absorb(firm_industry2#municipality) res(re_`t')

				unique firm_id if !mi(re_`t')
				gen n_firms = r(sum)


				sum n_firms
				local n_firms = r(mean)
		
			    foreach dep of var  wd3ln_amount_firm {
			
sum e_wnocore if !mi(re_`t') ,d
replace e_wnocore = e_wnocore*0.75/r(sd) if !mi(re_`t')
sum e_wnocore if !mi(re_`t') ,d
gen Noncore  = e_wnocore if !mi(re_`t')


				* Col 6
				reghdfe `dep' Noncore f`t' `risk_controls' $firm_controls  `bank_controls'   if !mi(re_`t') , absorb(firm_industry2#municipality) vce(cluster `clusters')
				outreg2 using Table13A, excel append label ctitle(Firm-level)  dec(3) drop(`risk_controls' $firm_controls  `bank_controls') ///
				 addtext( N firms, `n_firms') nocons	
				
		
								
				}
			 	}
				}


		restore
*End of Table 13A				

